//app.js
import Ajax from "./utils/base/ajax.js";
import { showToast } from "./utils/common.js";
const $ = new Ajax()
App({

    /**
     * 当小程序初始化完成时，会触发 onLaunch（全局只触发一次）
     */
    onLaunch (e) {

        console.log("App-->onLaunch", e)

    },

    /**
     * 当小程序启动，或从后台进入前台显示，会触发 onShow
     */
    onShow (e) {

        console.log("App-->onShow", e)

    },

    /**
     * 网络请求--获取用户信息
     */
    getUserInfo(cb) {

        var that = this

        if (that.globalData.userInfo) {

            typeof cb == "function" && cb(that.globalData.userInfo)

        } else {

            //调用登录接口
            wx.login({
                success: res1 => {

                    wx.getUserInfo({
                        success: res2 => {

                            // that.globalData.userInfo = res2.userInfo


                            // 数据准备
                            let data = {
                                code: res1.code,
                                iv: res2.iv,
                                rawData: res2.rawData,
                                signature: res2.signature,
                                encryptedData: res2.encryptedData,
                            }

                            $.post("index", data, res3 => {

                                console.log("网络请求--获取用户信息", res3)

                                if (res3.statusCode == 200 && res3.data.code == 200) {

                                    that.globalData.userInfo = res3.data.data
                                    typeof cb == "function" && cb(that.globalData.userInfo)

                                } else {

                                    showToast(res.data.msg || '网络错误')

                                }

                            })

                        }
                    })

                }
            })

        }

    },

    /**
     * 当小程序从前台进入后台，会触发 onHide
     */
    onHide () {

    },

    /**
     * 当小程序发生脚本错误，或者 api 调用失败时，会触发 onError 并带上错误信息
     */
    onError (msg) {

    },

    globalData: {
        userInfo: null
    }

})